#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    QDir dirs;
    currentpath="D:\\qtdown\\project\\XMLfile";
    currentname="\\newxmlfile.xml";

}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::writeXml()
{
    if(!openfile(currentname))
    {
        QMessageBox::critical(this,"打开文件","打开文件失败");
        return ;
    }
    QMessageBox::information(this,"打开文件","打开文件成功");
    QDomDocument dc;//创建xml文档
    QDomProcessingInstruction version;//设置版本号为头部
    version=dc.createProcessingInstruction("xml","version = \"1.0\" encoding = \"UTF-8\"");
    dc.appendChild(version);
    //设置根节点
    QDomElement root;
    root=dc.createElement("factory");
    dc.appendChild(root);
    //设置父节点
    QDomElement item=dc.createElement("information");
    {
        QDomElement id=dc.createElement("WNO");//创建子节点
        QDomText idtext=dc.createTextNode("WNO");//设置内容属于的节点
        idtext.setData(ui->lineEdit_write_id->text());
        id.appendChild(idtext);
        item.appendChild(id);

        QDomElement name=dc.createElement("WNAME");
        QDomText nametext=dc.createTextNode("WNAME");
        nametext.setData(ui->lineEdit_write_name->text());
        name.appendChild(nametext);
        item.appendChild(name);

        QDomElement sexy=dc.createElement("WSEXY");
        QDomText sexytext=dc.createTextNode("WSEXY");
        sexytext.setData(ui->lineEdit_write_sexy->text());
        sexy.appendChild(sexytext);
        item.appendChild(sexy);

        QDomElement dp=dc.createElement("WDP");
        QDomText dptext=dc.createTextNode("WDP");
        dptext.setData(ui->lineEdit_write_dp->text());
        dp.appendChild(dptext);
        item.appendChild(dp);

        QDomElement node5=dc.createElement("WDepartment"); // 创建子节点（创建元素节点）
        QDomText domtext5=dc.createTextNode("WDepartment"); // 可创建文本节点
        domtext5.setData(ui->lineEdit_write_department->text()); // 设置子节点数据
        node5.appendChild(domtext5); // 将子节点数据进行绑定
        item.appendChild(node5); // 将子节点绑定到父亲节点

        QDomElement node6=dc.createElement("WSalary"); // 创建子节点（创建元素节点）
        QDomText domtext6=dc.createTextNode("WSalary"); // 可创建文本节点
        domtext6.setData(ui->lineEdit_write_salary->text()); // 设置子节点数据
        node6.appendChild(domtext6); // 将子节点数据进行绑定
        item.appendChild(node6); // 将子节点绑定到父亲节点
    }
        root.appendChild(item);

         my_file.write(dc.toString().toLocal8Bit().data());
        my_file.close();
        QMessageBox::information(this,"提示","恭喜你，写入XML文件数据成功，请查看目录文件?",QMessageBox::Yes);


}

void Dialog::readXml()
{
    if(!openfile(currentname))
    {
        QMessageBox::critical(this,"打开文件","打开文件失败");
        return ;
    }
      QMessageBox::information(this,"打开文件","打开文件成功");
    QDomDocument dc;
     my_file.seek(0);
    QString errorMsg;
    int errorLine, errorColumn;
    if(!dc.setContent(&my_file, false, &errorMsg, &errorLine, &errorColumn))
    {
        QMessageBox::critical(this, "解析错误",
            QString("错误: %1\n行: %2\n列: %3").arg(errorMsg).arg(errorLine).arg(errorColumn));
        return;
    }

   QDomElement root=dc.documentElement();//读取根节点

   QDomNode node=root.firstChild();//读取父节点
    while(!node.isNull())
    {
        QDomNodeList list=node.childNodes();//获取所有子节点
        QString nodename=node.toElement().tagName();//读取父亲节点名称
        if(nodename.compare("information")==0)
        {
            readrootxml(list);
        }
        node=node.nextSibling();//node的下一个节点，而非子节点，而是同为父节点的节点
    }

}
void Dialog::readrootxml(QDomNodeList& ls)
{
    for(int i=0;i<ls.size();i++)
    {
        QDomElement child=ls.at(i).toElement();
        if(child.tagName().compare("WNO")==0)//取出子节点比较
        {
            ui->lineEdit_read_id->setText(child.text());
        }
        else if(child.tagName().compare("WNAME")==0)
        {
            ui->lineEdit_read_name->setText(child.text());
        }
        else if(child.tagName().compare("WSEXY")==0)
        {
            ui->lineEdit_read_sexy->setText(child.text());
        }
        else if(child.tagName().compare("WDP")==0)
        {
            ui->lineEdit_read_dp->setText(child.text());

        }
        else if(child.tagName().compare("WDepartment")==0)
        {
            ui->lineEdit_read_department->setText(child.text());
        }
        else if(child.tagName().compare("WSalary")==0)
        {
            ui->lineEdit_read_salary->setText(child.text());
        }
    }
}

bool Dialog::openfile(QString filename)
{
    QDir dir(currentpath);
       if (!dir.exists()) {
           dir.mkpath(".");
       }
    my_file.setFileName(currentpath+filename);
    return my_file.open(QFileDevice::ReadWrite|QFile::Text);

}


void Dialog::on_pushButton_clicked()
{
writeXml();
}

void Dialog::on_pushButton_2_clicked()
{
readXml();
}
